package com.ra.model.deal;

import com.ra.model.user.UserProfile;
import com.ra.model.product.Product;

import java.util.Date;
import java.util.Set;
import java.util.HashSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "ASK")
public class Ask {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="ASK_ID")
	private int id;
	
	public int getId() {
		return id;
	}
	
	@ManyToOne(targetEntity = com.ra.model.user.UserProfile.class)
	@JoinColumn(name = "USER_ID", nullable = false)
    private UserProfile user;
	
	public void setUserProfile(UserProfile user) {
	    this.user = user;	
	}
	
	public UserProfile getUserProfile() {
		return user;
	}
	
	/* It is possible that the user wants 
	 * something that is not in our catalogue.
	 * Allow the user to enter a description.
	 */
	@Column(name="ASK_DESCRIPTION")
	private String  askDescription;
	
	public void setAskDescription(String askDescription) {
		this.askDescription = askDescription;
	}
	
	public String getAskDescription() {
		return askDescription;
	}
	
	/*
	 * When the user wants a product from our catalogue
	 */
	@ManyToOne(targetEntity = com.ra.model.product.Product.class)
	@JoinColumn(name  = "PRODUCT_ID", nullable = true)
    private Product product;
    
	public void setProduct(Product product) {
		this.product = product;
	}
	
	public Product getProduct() {
		return product;
	}
	
	/*
	 * Dates for this ask
	 */
	@Column(name = "CREATION_DATE")
    private Date creationDate;

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}
	
	public Date getCreationDate() {
	    return creationDate;	
	}
	
	@Column(name="EXPIRATION_DATE")
	private Date expirationDate;
	
	public void setExpirationDate(Date expirationDate) {
		this.expirationDate = expirationDate;
	}
	
	public Date getExpirationDate() {
		return expirationDate;
	}
	
	/* We don't need a third type SINGLE_OR_GROUP
	 * as bidding for GROUP would include bidding
	 * for SINGLE as well
	 */
	public enum AskType {
		SINGLE, 
		GROUP
	}
	
	@Column(name = "ASK_TYPE")
	@Enumerated(EnumType.ORDINAL)
    private AskType askType;
	
	public void setAskType(Ask.AskType askType) {
		this.askType = askType;
	}
	
	public AskType getAskType() {
		return this.askType;
	}
	
	@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "ask")
	private Set<Bid> bids = new HashSet<Bid>();
	
	public void setBids(Set<Bid> bids){
		this.bids = bids;
	}
	
	public Set<Bid> getBids() {
		return bids;
	}
	
	public void addBid(Bid bid) {
		bids.add(bid);
		bid.setAsk(this);
	}
	
	@Column(name = "IS_ACTIVE")
	private boolean isActive;
	
	public void setIsActive(boolean isActive) {
		this.isActive = isActive;
	}
	
	public boolean getIsActive() {
		return isActive;
	}
}
